package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    //创建仓库
    TeacherDao dao = new TeacherDao();
    public boolean addTeacher(Teacher tea) {

        //调用库管对象的添加老师方法,将老师对象添加到数组容器
        boolean res = dao.addTeacher(tea);
        //将添加的信息返回
        return res;
    }
    //判断学号是否存在
    public boolean isExists(String id) {
        //指挥库管将数据仓库里所有老师信息都拿出来
        TeacherDao dao = new TeacherDao();
        Teacher[] allTeacher = dao.finAllTeacher();//{null,null,null,null,null}

        //判断库管返回的数组中有没有老师的工号和我这一次要筛选的工号一致
        //先假设没有人工号一样
        boolean exists = false;

        //遍历数组
        for (int i = 0; i < allTeacher.length;i++){
            //拿到一个学生对象
            Teacher tea = allTeacher[i];
            //当且仅当当前数组索引位置的老师对象不为null,并且这个工号和我们要匹配的工号一致时
            if (tea != null && tea.getId().equals(id)){
                //我们认为这个工号重复了,这个时候修改existsweitrue
                exists = true;
                break;
            }
        }
        //直接返回exists
        return exists;
    }

    //通过工号删除老师
    public void deleteTeacherById(String delId) {
        //指挥库管根据工号删除对应的老师
        dao.deleteTeacherById(delId);
    }

    //修改老师方法
    public void updateTeacher(String updateId, Teacher newTea) {
        dao.updateTeacher(updateId,newTea);
    }

    //查看老师方法
    public Teacher[] findAllTeacher() {
        //调用库管对象的findAllTeacher获取老师对象数组
       Teacher[] allTeacher = dao.findAllTeacher();
        //判断数组中是否有老师信息
        //
        boolean flag = false;
        for (int i = 0; i < allTeacher.length; i++) {
            Teacher tea = allTeacher[i];
            if (tea != null){
                flag = true;
                break;
            }
        }
        if (flag){
            return allTeacher;
        }else {
            return null;
        }
    }
}
